﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Forms;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace PROJECT_BOMBER.BLL
{
    class PlayState2 : GameState
    {

        List<Piece> pieces = new List<Piece>();

        SpriteFont spritetext;

        String text = "";
        String text2 = "";
        String text3 = "angle";

        Texture2D background;
        Texture2D test;

        Character character1;

        //Character character2;

        Level leveltest;

        Camera2d cam;

        Vector2 screen;

        Song musique;

        SoundEffect soundPiece;

        float AspectRatio;
        Microsoft.Xna.Framework.Point OldWindowSize;

        Microsoft.Xna.Framework.Rectangle screenRectangle;


        public override void Initialize()
        {

            screen = new Vector2(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);

            cam = new Camera2d(Globals.GDM, 0.75f);

            Game game = Globals.Game;

            #region character1

            character1 = new Character(50, -1100)
            {
                Speedmax = new Vector2(540.0f, 4150.0f),
                Speedmin = new Vector2(100.0f, 50.0f),
                Inertia = 10,
                Gravity = 3.6f,
                Controller = new ControllerKeyboard()
            };

            AnimationDefinition imgup = new AnimationDefinition("up", new Microsoft.Xna.Framework.Point(120, 100), new Microsoft.Xna.Framework.Point(1, 1), 10, false);
            AnimationDefinition imgup2 = new AnimationDefinition("ghost/jump", new Microsoft.Xna.Framework.Point(94, 99), new Microsoft.Xna.Framework.Point(1, 1), 10, false);
            AnimationDefinition imgdown = new AnimationDefinition("down", new Microsoft.Xna.Framework.Point(120, 100), new Microsoft.Xna.Framework.Point(1, 1), 10, false);
            AnimationDefinition imgdown2 = new AnimationDefinition("ghost/jump", new Microsoft.Xna.Framework.Point(94, 99), new Microsoft.Xna.Framework.Point(1, 1), 10, false);
            AnimationDefinition imgface = new AnimationDefinition("face", new Microsoft.Xna.Framework.Point(120, 100), new Microsoft.Xna.Framework.Point(1, 1), 10, false);
            AnimationDefinition imgface2 = new AnimationDefinition("ghost/face", new Microsoft.Xna.Framework.Point(94, 99), new Microsoft.Xna.Framework.Point(1, 1), 10, false);
            AnimationDefinition imgright = new AnimationDefinition("right", new Microsoft.Xna.Framework.Point(120, 100), new Microsoft.Xna.Framework.Point(4, 1), 10, true);
            AnimationDefinition imgright2 = new AnimationDefinition("ghost/right", new Microsoft.Xna.Framework.Point(94, 99), new Microsoft.Xna.Framework.Point(4, 1), 10, true);
            AnimationDefinition imgleft = new AnimationDefinition("left", new Microsoft.Xna.Framework.Point(120, 100), new Microsoft.Xna.Framework.Point(4, 1), 10, true);
            AnimationDefinition imgleft2 = new AnimationDefinition("ghost/left2", new Microsoft.Xna.Framework.Point(94, 99), new Microsoft.Xna.Framework.Point(4, 1), 10, true);
            AnimationDefinition imgfly = new AnimationDefinition("fly", new Microsoft.Xna.Framework.Point(120, 200), new Microsoft.Xna.Framework.Point(1, 1), 10, true);
            AnimationDefinition imgattack = new AnimationDefinition("attack", new Microsoft.Xna.Framework.Point(120, 100), new Microsoft.Xna.Framework.Point(2, 1), 20, true);
            AnimationDefinition imgdead = new AnimationDefinition("dead", new Microsoft.Xna.Framework.Point(120, 100), new Microsoft.Xna.Framework.Point(1, 1), 10, false);
            AnimationDefinition imgfacecowboy = new AnimationDefinition("Cowboy/face", new Microsoft.Xna.Framework.Point(123, 150), new Microsoft.Xna.Framework.Point(1, 1), 10, true);
            AnimationDefinition imgflycowboy = new AnimationDefinition("Cowboy/fly", new Microsoft.Xna.Framework.Point(156, 150), new Microsoft.Xna.Framework.Point(1, 1), 10, true);
            AnimationDefinition imgrightcowboy = new AnimationDefinition("Cowboy/right", new Microsoft.Xna.Framework.Point(123, 150), new Microsoft.Xna.Framework.Point(14, 1), 60, true);
            AnimationDefinition imgleftcowboy = new AnimationDefinition("Cowboy/left", new Microsoft.Xna.Framework.Point(123, 150), new Microsoft.Xna.Framework.Point(14, 1), 60, true);


            character1.AddAnimation("default", new AnimationSprite(game, imgface));
            character1.AddAnimation("right", new AnimationSprite(game, imgright));
            character1.AddAnimation("left", new AnimationSprite(game, imgleft));
            character1.AddAnimation("up", new AnimationSprite(game, imgup));
            character1.AddAnimation("down", new AnimationSprite(game, imgdown));
            character1.AddAnimation("fly", new AnimationSprite(game, imgfly));
            character1.AddAnimation("dead", new AnimationSprite(game, imgdead));
            character1.AddAnimation("attack", new AnimationSprite(game, imgattack));

            character1.InitializeAnimations();

            /*character2 = new Character(250, -900)
            {
                Speedmax = new Vector2(500.0f, 5000.0f),
                Speedmin = new Vector2(1000.0f, 90.0f),
                Controller = new ControllerKeyboard2()
            };

            character2.AddAnimation("default", new AnimationSprite(game, imgface2));
            character2.AddAnimation("right", new AnimationSprite(game, imgright2));
            character2.AddAnimation("left", new AnimationSprite(game, imgleft2));
            character2.AddAnimation("up", new AnimationSprite(game, imgup2));
            character2.AddAnimation("down", new AnimationSprite(game, imgdown2));
            character2.AddAnimation("fly", new AnimationSprite(game, imgfly));
            character2.AddAnimation("dead", new AnimationSprite(game, imgdead));
            character2.AddAnimation("attack", new AnimationSprite(game, imgattack));


            character2.InitializeAnimations();*/


            #endregion

            #region Level

            leveltest = new Level();


            AnimationDefinition imglevel = new AnimationDefinition("grotte-background", new Microsoft.Xna.Framework.Point(3000, 2561), new Microsoft.Xna.Framework.Point(1, 1), 10, true);

            // Images du niveau 1
            AnimationDefinition imglvl11b = new AnimationDefinition("Level Tutorial/level1-1-b", new Microsoft.Xna.Framework.Point(3001,3000), new Microsoft.Xna.Framework.Point(1, 1), 10, true);
            AnimationDefinition imglvl11 = new AnimationDefinition("Level Tutorial/level1-1", new Microsoft.Xna.Framework.Point(3001, 3000), new Microsoft.Xna.Framework.Point(1, 1), 10, true);
            AnimationDefinition imglvl11f = new AnimationDefinition("Level Tutorial/level1-1-f", new Microsoft.Xna.Framework.Point(3001, 3000), new Microsoft.Xna.Framework.Point(1, 1), 10, true);

            AnimationDefinition imglvl12b = new AnimationDefinition("Level Tutorial/level1-2-b", new Microsoft.Xna.Framework.Point(3001, 3000), new Microsoft.Xna.Framework.Point(1, 1), 10, true);
            AnimationDefinition imglvl12 = new AnimationDefinition("Level Tutorial/level1-2", new Microsoft.Xna.Framework.Point(3001, 3000), new Microsoft.Xna.Framework.Point(1, 1), 10, true);
            AnimationDefinition imglvl12f = new AnimationDefinition("Level Tutorial/level1-2-f", new Microsoft.Xna.Framework.Point(3001, 3000), new Microsoft.Xna.Framework.Point(1, 1), 10, true);

            AnimationDefinition imglvl13b = new AnimationDefinition("Level Tutorial/level1-3-b", new Microsoft.Xna.Framework.Point(3001, 3000), new Microsoft.Xna.Framework.Point(1, 1), 10, true);
            AnimationDefinition imglvl13 = new AnimationDefinition("Level Tutorial/level1-3", new Microsoft.Xna.Framework.Point(3001, 3000), new Microsoft.Xna.Framework.Point(1, 1), 10, true);
            AnimationDefinition imglvl13f = new AnimationDefinition("Level Tutorial/level1-3-f", new Microsoft.Xna.Framework.Point(3001, 3000), new Microsoft.Xna.Framework.Point(1, 1), 10, true);



            leveltest.addFrame(imglvl11, imglvl11b, imglvl11f, Globals.Game);
            leveltest.addFrame(imglvl12, imglvl12b, imglvl12f, Globals.Game);
            leveltest.addFrame(imglvl13, imglvl13b, imglvl13f, Globals.Game);


            leveltest.InitializeAnimation();


            #endregion



            #region filtre no_light

            AnimationDefinition img_no_light = new AnimationDefinition("Level Tutorial/level0", new Microsoft.Xna.Framework.Point(3900, 3900), new Microsoft.Xna.Framework.Point(1, 1), 10, true);

            #endregion

            #region ventilateurs


            // Pièces
            AnimationDefinition imgpiece = new AnimationDefinition("pieces/face", new Microsoft.Xna.Framework.Point(37, 72), new Microsoft.Xna.Framework.Point(2, 1), 2, true);

            Piece piece;

            for (int i = 0; i < 50; i++)
            {
                piece = new Piece(70 * i, 1500, 10);

                pieces.Add(piece);

                pieces[i].AddAnimation("default", new AnimationSprite(game, imgpiece));

                pieces[i].InitializeAnimations();


                /*pieces[i].Animations.Base(imgpiece, game);
                pieces[i].Animations.Base().Initialize();*/

            }

            #endregion
        }

        public override void Update(float deltatime)
        {
            screenRectangle = new Microsoft.Xna.Framework.Rectangle(0, 0, Globals.GDM.PreferredBackBufferWidth, Globals.GDM.PreferredBackBufferHeight);

            #region listes d'element pour les collision element se deplaçant

            List<ElementPhysique> elements = new List<ElementPhysique>();
            //elements.Add(ventilo1);
            elements.Add(leveltest);

            for (int i = 0; i < 50; i++)
            {
                elements.Add(pieces[i]);
            }

            #endregion

            character1.Update(leveltest, elements);

            //character2.Update(this, Globals.GameTime, Globals.GDM, leveltest, cam, elements, screen);

            cam.Update(Globals.GDM, character1);

            //piece.AnimationSprite.Base().Framerate = 1;

            for (int i = 0; i < 50; i++)
            {
                pieces[i].Update(leveltest);
            }

            leveltest.Update();
            //piece.AnimationSprite.Base().Framerate = 1;


            /*if (character2.Case.Intersects(character1.Case))
            {
                character2.CollisionStrategy.Collision(character1, character2);
            }*/

            text = string.Format("Vitesse : X = {0}   Y = {1}", character1.Speed.X.ToString(), character1.Speed.Y.ToString());
            text2 = string.Format("Position : X = {0}   Y = {1}", character1.Position.X, character1.Position.Y);
            text3 = string.Format("Piece frame : {0} ", pieces[0].Animations["default"].Framerate);

            KeyboardState ks = Keyboard.GetState();
            if (ks.IsKeyDown(Microsoft.Xna.Framework.Input.Keys.K))
            {
                Globals.GameStateStack.Set(new LoadState(new PlayState()));
            }

            if (ks.IsKeyDown(Microsoft.Xna.Framework.Input.Keys.M))
            {
                MediaPlayer.Pause();

            }

            if (ks.IsKeyDown(Microsoft.Xna.Framework.Input.Keys.U))
            {
                MediaPlayer.Resume();

            }
        }

        public override void Draw()
        {
            Globals.GDM.GraphicsDevice.Clear(Microsoft.Xna.Framework.Color.Gray);

            Globals.SpriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend);
            Globals.SpriteBatch.Draw(background, screenRectangle, Microsoft.Xna.Framework.Color.White);
            Globals.SpriteBatch.End();

            Globals.SpriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend, null, null, null, null, cam.get_transformation(Globals.GDM.GraphicsDevice));
            leveltest.DrawAnimation(Globals.GameTime, 0);
            Globals.SpriteBatch.End();

            Globals.SpriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend, null, null, null, null, cam.get_transformation(Globals.GDM.GraphicsDevice));
            leveltest.DrawAnimation(Globals.GameTime, 1);
            Globals.SpriteBatch.End();

            Globals.SpriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend, null, null, null, null, cam.get_transformation(Globals.GDM.GraphicsDevice));
            leveltest.DrawAnimation(Globals.GameTime, 2);
            Globals.SpriteBatch.End();

            Globals.SpriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend, null, null, null, null, cam.get_transformation(Globals.GDM.GraphicsDevice));
            character1.DrawAnimation();
            Globals.SpriteBatch.End();

            Globals.SpriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend, null, null, null, null, cam.get_transformation(Globals.GDM.GraphicsDevice));
            //character2.DrawAnimation(Globals.GameTime);
            Globals.SpriteBatch.End();

            for (int i = 0; i < 50; i++)
            {
                Globals.SpriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend, null, null, null, null, cam.get_transformation(Globals.GDM.GraphicsDevice));
                pieces[i].DrawAnimation();
                Globals.SpriteBatch.End();
            }

            

            Globals.SpriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend);

            Globals.SpriteBatch.DrawString(spritetext, text, Vector2.Zero, Microsoft.Xna.Framework.Color.Red);
            Globals.SpriteBatch.DrawString(spritetext, text2, new Vector2(400, 0), Microsoft.Xna.Framework.Color.Red);
            Globals.SpriteBatch.DrawString(spritetext, text3, new Vector2(800, 0), Microsoft.Xna.Framework.Color.Red);



            Globals.SpriteBatch.End();
        }

        private Vector2 PlayTextPosition = Vector2.Zero;


        public override void Load()
        {
            musique = Globals.Content.Load<Song>("Music");

            soundPiece = Globals.Content.Load<SoundEffect>("Coin");

            AspectRatio = Globals.GDM.GraphicsDevice.Viewport.AspectRatio;
            OldWindowSize = new Microsoft.Xna.Framework.Point(Globals.Window.ClientBounds.Width, Globals.Window.ClientBounds.Height);

            // Create a spritetext, which can be used to write.
            spritetext = Globals.Content.Load<SpriteFont>("Arial");

            background = Globals.Content.Load<Texture2D>("Level Tutorial/level-background");
            test = Globals.Content.Load<Texture2D>("test");

            character1.LoadAnimations(Globals.SpriteBatch);
            //character2.LoadAnimations(Globals.SpriteBatch);

            for (int i = 0; i < 50; i++)
            {
                pieces[i].LoadAnimations(Globals.SpriteBatch);
            }

            leveltest.LoadAnimations(Globals.SpriteBatch);
        }

        public override void Unload()
        {
            Globals.Content.Unload();
        }
    }
}
